package com.csxy.nyf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.csxy.nyf.entity.EduSubject;
import com.csxy.nyf.entity.excel.SubjetcData;
import com.csxy.nyf.entity.vo.OneSubject;
import com.csxy.nyf.entity.vo.TwoSubject;
import com.csxy.nyf.exception.MyException;
import com.csxy.nyf.listener.SubjectExcelListtener;
import com.csxy.nyf.mapper.EduSubjectMapper;
import com.csxy.nyf.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类，parent_id=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类，parent_id!=0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        //创建list集合，用于最终数据封装
        ArrayList<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到一级分类list集合每个对象，获取每个一级分类对象值
        //封装到要求的最终list集合中
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //遍历oneSubjectList集合
            //得到oneSubjectList集合每个eduSubject对象
            EduSubject oSubject = oneSubjectList.get(i);
            //edusubject对象值获取出来,放到oneSubject对象里面
            OneSubject oneSubject = new OneSubject();
            //把eduSubject对象值复制到oneSubject对象里面去（要求两个类的复制注入的属性名一致）
            BeanUtils.copyProperties(oSubject,oneSubject);
            //多个oneSubject放到finalSubjectList里面
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有二级分类
            //创建list集合，用于封装每一个一级分类中所有的二级分类
            ArrayList<TwoSubject> finalTwoSubjects = new ArrayList<>();

            //遍历二级list分类集合
            for (int j = 0; j < twoSubjectList.size(); j++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(j);
                //判断二级分类parentId和一级分类id是否一样
                if (tSubject.getParentId().equals(oneSubject.getId())) {
                    //把twoSubject值复制到TwoSubject，最终放到twoSubjectList中
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    finalTwoSubjects.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到oneSubject中
            oneSubject.setChildren(finalTwoSubjects);
        }
        return finalSubjectList;
    }

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        //添加课程分类
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();

            //调用easyexcel的方法进行读取
            EasyExcel.read(inputStream, SubjetcData.class,(ReadListener) new SubjectExcelListtener(eduSubjectService))
                    .sheet().doRead();
        }catch (Exception  e){
            throw new MyException(20002,"添加课程分类失败！");
        }

    }
}
